package cn.file.test;

import javafx.scene.control.RadioMenuItem;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RanDomText {
    public static void main(String args[]) throws IOException {
        File file=new File("shan.txt");

        /*需要文件的大小len，开始beginPos,实际大小actualSize
        指定每次的传输大小，计算出组数，和最后的剩余量
        组数：文件大小除传输大小，向上取整，（还有最后的余量）
        余量：文件大小减去（组数乘以文件传输大小减去一）
        */
         //文件大小
        long len=file.length();
        //每次传输的大小
        int a=110;
        //组数
        int count=(int)Math.ceil(len*1.0/a);
        byte[] bytes=new byte[a];
        int beginPos=0;
        int actualSize=(int)(a>len?len:a);
        RandomAccessFile rw;


        for(int i=0;i<count;i++){
            beginPos=a*i;
            if(i==count-1){
             actualSize=(int)len;
            }else{
             actualSize=a;
             len-=actualSize;
            }
            randomTest(file,beginPos,actualSize,bytes);
            File file01=new File(i+"wo.txt");
            rw=new RandomAccessFile(file01,"rw");
            rw.write(bytes,0,actualSize);

       //System.out.println(i+"  "+beginPos+" "+actualSize);
        }

    }
    public static void randomTest(File file,int beginPro,int actulSize,byte[] bytes) throws IOException {

        RandomAccessFile raf=new RandomAccessFile(file,"r");

        int len=0;
        raf.seek(beginPro);
        while ((len=raf.read(bytes))!=-1){
            if(len>actulSize){
                System.out.print(new String(bytes,0,actulSize));
                break;
            }else{
                System.out.print(new String(bytes,0,len));
                actulSize-=len;

            }
        }
        if(raf!=null){
            raf.close();
        }

    }
}
class Test01{
    public static void main(File file,int beginPro,int actulSize) throws IOException {

        RandomAccessFile raf=new RandomAccessFile(file,"r");
        byte[] bytes=new byte[110];
        int len=0;
        raf.seek(beginPro);
        while ((len=raf.read(bytes))!=-1){
            if(len>actulSize){
                System.out.print(new String(bytes,0,actulSize));
                break;
            }else{
                System.out.print(new String(bytes,0,len));
                actulSize-=len;

            }
        }
        if(raf!=null){
            raf.close();
        }

    }
}
